//do \\c3\rdat\SHoude\Research\EEgap\EEgap_scripts\Select_Available_Week_Pair_Match_vtoggle.do pair_version

global pathname="raw_data_folder"

//local pair_version = `"`1'"'
/*
j=4(1)5
pairs_matched_`j'_size2only_wmiss_kwh
pairs_matched_`j'_size2only_nomiss_kwh
pairs_matched_`j'_size2only_impute_kwh
*/
use $pathname\lcidemo_046_2008_2012_allsales, clear

	drop if year==2007

	mvencode o_qty,mv(1) over
	collapse(sum) o_qty (median) retail promo,by(pid zipcode week year)
	
//Identify week-zipcode where the products are available 	
	gen week_num=week
	replace week_num=week_num+52 if year==2009
	replace week_num=week_num+104 if year==2010
	replace week_num=week_num+156 if year==2011
	replace week_num=week_num+208 if year==2012
	egen pid_zip=group(pid zipcode)

preserve
	collapse(sum) o_qty,by(pid zip pid_zip)
	ren 	o_qty sales_pid_zip
	sort pid_zip 
	save $pathname\sales_pid_zip_tmp, replace
restore	
	
	tsset pid_zip week_num
	tsfill
	
	mvencode o_qty,mv(0) over
	
	drop zip pid
	sort pid_zip
	merge pid_zip using $pathname\sales_pid_zip_tmp
	tab _m
	drop _m
	
	sort pid
	merge pid using  $pathname\pairs_matched_2_size2only_manual_check_YQ_SH
	//merge pid using  $pathname\pairs_matched_4_size2restricted
	tab _m
	keep if _m==3
	drop _m

		
	replace year=2008 if week_num<=52
	replace year=2009 if week_num>52 & week_num<=104 
	replace year=2010 if week_num>104 & week_num<=156
	replace year=2011 if week_num>156 & week_num<=208
	replace year=2012 if week_num>208 
	
	replace week=week_num
	replace week=week_num-52 if year==2009
	replace week=week_num-104 if year==2010
	replace week=week_num-156 if year==2011
	replace week=week_num-208 if year==2012

	gen time_since_pid_zip=0
	sort pid zip week_num
	by pid zipcode: replace time_since_pid_zip=time_since_pid_zip[_n-1]+1 if o_qty==0
	by pid zipcode: egen  max_time_since=max(time_since_pid_zip)	 

/*	
	sort pid_zip week_num
	gen Lag1_qty=L.o_qty
	gen For1_qty=F.o_qty
	gen Lag2_qty=L2.o_qty
	gen For2_qty=F2.o_qty
*/	

	gen avail_week_1_pid=0
	replace avail_week_1_pid=1 if time_since_pid_zip<=1 & (time_since_pid_zip<max_time_since)
	replace avail_week_1_pid=1 if o_qty>0
	//replace avail_week_1_pid=1 if Lag1_qty>0 & For1_qty>0  & Lag1_qty!=. & For1_qty!=.
	
	gen avail_week_2_pid=0
	replace avail_week_2_pid=1 if time_since_pid_zip<=2 & (time_since_pid_zip<max_time_since)
	replace avail_week_2_pid=1 if o_qty>0
	//replace avail_week_2_pid=1 if (Lag1_qty>0 | Lag2_qty>0) &  (For1_qty>0 | For2_qty>0) & ((Lag1_qty!=. & For1_qty!=.) | (Lag2_qty!=. & For2_qty!=.) | (Lag1_qty!=. & For2_qty!=.) | (Lag2_qty!=. & For1_qty!=.)   )
	
	gen avail_week_4_pid=0
	replace avail_week_4_pid=1 if time_since_pid_zip<=4 & (time_since_pid_zip<max_time_since)
	replace avail_week_4_pid=1 if o_qty>0
	
	gen avail_week_6_pid=0
	replace avail_week_6_pid=1 if time_since_pid_zip<=6 & (time_since_pid_zip<max_time_since)
	replace avail_week_6_pid=1 if o_qty>0
	
	gen avail_week_8_pid=0
	replace avail_week_8_pid=1 if time_since_pid_zip<=8 & (time_since_pid_zip<max_time_since)
	replace avail_week_8_pid=1 if o_qty>0
	
	gen avail_week_12_pid=0
	replace avail_week_12_pid=1 if time_since_pid_zip<=12 & (time_since_pid_zip<max_time_since)
	replace avail_week_12_pid=1 if o_qty>0
	
	gen avail_week_all_pid=0
	replace avail_week_all_pid=1 if time_since_pid_zip<max_time_since
	replace avail_week_all_pid=1 if o_qty>0
	
	
	sort pid_pair_2_wmiss_kwh zipcode week year	
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_1_pair_tmp=sum(avail_week_1_pid)
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_2_pair_tmp=sum(avail_week_2_pid)
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_4_pair_tmp=sum(avail_week_4_pid)
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_6_pair_tmp=sum(avail_week_6_pid)
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_8_pair_tmp=sum(avail_week_8_pid)
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_12_pair_tmp=sum(avail_week_12_pid)
	by pid_pair_2_wmiss_kwh zipcode week year: egen avail_week_all_pair_tmp=sum(avail_week_all_pid)
	
	tab  avail_week_1_pair_tmp
	tab  avail_week_2_pair_tmp
	tab  avail_week_4_pair_tmp
	tab  avail_week_6_pair_tmp
	tab  avail_week_8_pair_tmp
	tab  avail_week_12_pair_tmp
	tab  avail_week_all_pair_tmp
	
	gen avail_week_1_pair=0
	replace avail_week_1_pair=1 if  avail_week_1_pair_tmp==2
	
	gen avail_week_2_pair=0
	replace avail_week_2_pair=1 if  avail_week_2_pair_tmp==2
	
	gen avail_week_4_pair=0
	replace avail_week_4_pair=1 if  avail_week_4_pair_tmp==2
	
	gen avail_week_6_pair=0
	replace avail_week_6_pair=1 if  avail_week_6_pair_tmp==2
	
	gen avail_week_8_pair=0
	replace avail_week_8_pair=1 if  avail_week_8_pair_tmp==2
	
	gen avail_week_12_pair=0
	replace avail_week_12_pair=1 if  avail_week_12_pair_tmp==2
	
	gen avail_week_all_pair=0
	replace avail_week_all_pair=1 if  avail_week_all_pair_tmp==2
	
	keep pid_pair_2_wmiss_kwh zipcode week year avail_week_2_pair avail_week_1_pair avail_week_4_pair avail_week_6_pair avail_week_8_pair avail_week_12_pair  avail_week_all_pair
	sort pid_pair_2_wmiss_kwh zipcode week year
save $pathname\Available_Week_pairs_matched_2_size2only_manual_check_YQ_SH, replace	
	
tab avail_week_2_pair 
tab avail_week_1_pair 
tab avail_week_4_pair 
tab avail_week_6_pair 
tab avail_week_8_pair 
tab avail_week_12_pair  
tab avail_week_all_pair

/*
tab avail_week_1

avail_week_ |
          1 |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |  5,635,044       93.71       93.71
          1 |    378,062        6.29      100.00
------------+-----------------------------------
      Total |  6,013,106      100.00
*/

/*
tab avail_week_2

avail_week_ |
          2 |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |  5,219,594       86.80       86.80
          1 |    793,512       13.20      100.00
------------+-----------------------------------
      Total |  6,013,106      100.00

*/
